<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from "better-scroll"

  export default {
    name: "Scroll",
    data() {
      return {
        scroll: null,
        msg: "哈哈哈"
      }
    },
    props: {
      probeType: {
        type: Number,
        default: 0
      },
      pullUpLoad: {
        type: Boolean,
        default: false
      }
    },
    mounted() {
      //this.$refs.wrapper
      // const content = document.querySelector('.wrapper')
      //一.创建BScroll对象
      this.scroll = new BScroll(this.$refs['wrapper'], {
        //如果Best-scroll里面有div，必须设置click：true才能点击，而button无论是true还是false都可以点击
        click: true,
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad
      });
      //二.监听滚动的位置
      if (this.probeType === 2 || this.probeType === 3) {
        this.scroll.on('scroll', (position) => {
          this.$emit("scroll", position);
        });
      }
      //三.监听上拉事件
      if (this.pullUpLoad) {
        this.scroll.on('pullingUp', () => {
          this.$emit("pullingUp");
        });
      }
      //this.scroll.scrollTo(0, 0);
      //console.log(this.scroll);
      //this.scroll.refresh();
    },
    methods: {
      //下面是ES6的写法，默认传值
      /*scrollTo(x, y, time = 1000)
      {
        this.scroll.scrollTo(x, y, time)
      }*/
      finishPullUp() {
        this.scroll.finishPullUp()
      },
      refresh() {
        //console.log("------");
        this.scroll && this.scroll.refresh();
      }
    }
  }
</script>

<style scoped>

</style>
